package com.mckevitt.running.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.mckevitt.running.domain.Contact;
import com.mckevitt.running.service.ContactService;

@Controller
@SessionAttributes
public class ContactController {

	@Autowired
    private ContactService contactService;
	
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("contact")
                            Contact contact, BindingResult result) {
 
        System.out.println("First Name:" + contact.getFirstname() +
                    "Last Name:" + contact.getLastname());
        
        contactService.addContact(contact);
 
        return "redirect:contacts.html";
    }
    
    @RequestMapping("/contacts")
    public String listContacts(Map<String, Object> map)	{
        map.put("contact", new Contact());
        map.put("contactList", contactService.listContact());
 
        return "contact";		
	}
    
    @RequestMapping("/delete/{contactId}")
    public String deleteContact(@PathVariable("contactId")
    Integer contactId) {
 
        contactService.removeContact(contactId); 
        return "redirect:/contacts.html";
    }

        
    
    /*@RequestMapping("/contacts")
    public ModelAndView showContacts() {
 
        return new ModelAndView("contact", "command", new Contact());
    }*/
	
}
